package nc;
import java.io.BufferedReader;
import java.io.InputStreamReader;
import java.util.*;
public class Weizhong {
    
    static class Main1 {
        public static void main(String[] args) {
            Scanner sc = new Scanner(System.in);
            int n = sc.nextInt();
            int[] heights = new int[n];
            int max = 0;
            for (int i = 0; i < n; i++) {
                heights[i] = sc.nextInt();
                max = Math.max(max, heights[i]);
            }
            sc.close();
            int[] arr = new int[max];
            int[] res = new int[n];
            for (int i = n - 1; i >= 0; i--) {
                for (int j = 0; j < heights[i]; j++) {
                    res[n - 1 - i]++;
                    arr[j]++;
                }
            }
        
        }


    }

    

static class Main{
        public static void main(String[] args) throws Exception {
            BufferedReader br=new BufferedReader(new InputStreamReader(System.in));
            int n= Integer.parseInt(br.readLine());
            int[] arr=new int[n];
            String[] sp=br.readLine().split(" ");
            for (int i = 0; i <n ; i++) {
                arr[i]=Integer.parseInt(sp[i]);
            }
            int ans= process(3,0,Integer.MAX_VALUE,arr);
            System.out.println(ans);
        }
        public static int process(int rest , int i , int preChose , int[] arr){
            if (rest==0){
                return 1;
            }
            if (i>=arr.length){
                return 0;
            }
            if (preChose!=Integer.MAX_VALUE&& preChose>arr[i]){
                return process(rest,i+1,preChose,arr);
            }
            int res=0;//返回的答案
            int p1=process(rest-1,i+1,arr[i],arr);
            int p2=process(rest,i+1,preChose,arr);
            res=p1+p2;
            return res;
        }
    }

}
